<?php

class Backend_Base_Controller extends Zend_Controller_Action
{

    public function init()
    {

        // Resolve page title

        // Add general page title
        $page_title = $this->view->translate('Application name');
        $this->view->headTitle()->setSeparator(' / ');
        $this->view->headTitle($page_title);
        $this->view->headTitle($this->view->translate('Management'));

        // Try to get controller & action - specific page title
        $request        = $this->getRequest();
        $page_title_key = $request->getControllerName() . '/' . $request->getActionName();
        if ($page_title_key != $this->view->translate($page_title_key)) {
            $this->view->headTitle($this->view->translate($page_title_key));
        }

        // Set charset and disable client-side caching
        $this->view->headMeta()->appendHttpEquiv('Content-Type',  'text/html; charset=UTF-8');
//        $this->view->headMeta()->appendHttpEquiv('expires',       'Wed, 26 Feb 1997 08:21:57 GMT');
//        $this->view->headMeta()->appendHttpEquiv('pragma',        'no-cache');
//        $this->view->headMeta()->appendHttpEquiv('Cache-Control', 'no-cache');

        $location = $request->getControllerName() . '/' . $request->getActionName();
        $session  = Zend_Registry::get('session');
        if (empty($session->id) && $location != 'index/login') {
            $redirector = new Zend_Controller_Action_Helper_Redirector;
            $redirector->gotoUrl('/index/login')->redirectAndExit();
        }

        $request = $this->getRequest();
        if ($request->getParam('lang'))
        {
            $languages_list = Application_Model_Language::getList();
            if (is_array($languages_list))
            {
                foreach ($languages_list as $lang)
                {
                    if ($lang->getKey() == $request->getParam('lang'))
                    {
                        $session       = Zend_Registry::get('session');
                        $session->lang = $lang->getKey();
                        $this->_helper->message($this->view->translate('Language was successfully changed!'), Backend_Flash_Messenger::TYPE_INFO);
                        $redirector = new Zend_Controller_Action_Helper_Redirector;
                        $redirector->gotoUrl($request->getScheme() . '://' . $request->getHttpHost() . $request->getRequestUri())->redirectAndExit();
                    }
                }
            }
        }
    }

}